package com.szitrus.smp.scalseal.controller;

import com.google.common.collect.Maps;
import com.szitrus.smp.scalseal.entity.SealDO;
import com.szitrus.smp.scalseal.exception.BusinessException;
import com.szitrus.smp.scalseal.service.ScaleSealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author ssl
 * @create 2020/09/21 15:03
 */
@RestController
public class ScaleSealController {
    private static final String LINE = System.getProperty("line.separator");
    @Autowired
    private ScaleSealService scaleSealService;

    @GetMapping({"/", "index", ""})
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("index");

        return mv;
    }

    @PostMapping("scale")
    public Map<String, Object> scale(@RequestParam("sql") String sql,
                                     @RequestParam("width") Integer width,
                                     @RequestParam("height") Integer height) {
        Map<String, Object> result = Maps.newHashMap();
        result.put("code", "0");
        List<SealDO> list;
        try {
            list = scaleSealService.listSeal(sql);
        } catch (BusinessException e) {
            e.printStackTrace();
            result.put("msg", e.getErrorCode().getMsg());
            return result;
        }
        StringBuilder content = new StringBuilder();
        try {
            for (SealDO sealDO : list) {
                sealDO.setWidth(width);
                sealDO.setHeight(height);
                scaleSealService.updateSeal(sealDO);
                content.append("印章id[" + sealDO.getId() + "]缩放成功。");
                content.append(LINE);
            }
        } catch (BusinessException e) {
            e.printStackTrace();
            result.put("msg", e.getErrorCode().getMsg());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("msg", e.getMessage());
            return result;
        }
        result.put("code", "1");
        result.put("msg", content.toString());
        return result;

    }
}
